package com.PracticeSSm.controller;

import com.PracticeSSm.pojo.AdminLogin;
import com.PracticeSSm.service.AdminLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author cc
 * @date 2021/8/20 19:22
 * @Description
 */
@Controller
@RequestMapping("/admin")
public class adminController {
    @Autowired
    AdminLoginService adminLoginService;
    /**
     * 用户登录
     * @author sosmcc
     * @param
     */
//    @RequestBody请求体不能是表单只能是json 如果加了注解并且前端用的还是表单请求，就会报415错误
    @RequestMapping("/login")
    public void Login(HttpServletResponse resp, HttpSession session,AdminLogin adminLogin) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println(adminLogin);
        AdminLogin findadmin = adminLoginService.findadmin(adminLogin);
        String syscode = session.getAttribute("syscode").toString();
        if (syscode.equalsIgnoreCase(adminLogin.getVerifiy())){
            if (findadmin!=null){
                if (findadmin.getPassword().equals(adminLogin.getPassword())){
                    System.out.println("登陆成功");
                    session.setAttribute("loginAdmin",findadmin);
                    resp.getWriter().write("<script>location.href='/index';</script>");
                }else {
                    System.out.println("登录失败");
                    resp.getWriter().write("<script>alert('账号或密码错误！');location.href='/';</script>");
                }
            }else {
                resp.getWriter().write("<script>alert('没有该用户！');location.href='/';</script>");
            }
        }else {
            resp.getWriter().write("<script>alert('验证码输入如错误！');location.href='/';</script>");
        }
    }
}
